package com.shrw.duke.serialport.common.runnable;

import android.util.Log;

import com.shrw.duke.serialport.utils.HexUtils;

import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * Created by rw-duke on 2018/4/8.
 */

public class PortWriteRunnable implements Runnable {
    private static final String TAG = PortWriteRunnable.class.getSimpleName();
    private OutputStream mOutput;
    private byte[] mInstructions;

    public PortWriteRunnable(FileDescriptor fileDescriptor, byte[] instructions) {
        mOutput = new FileOutputStream(fileDescriptor);
        this.mInstructions = instructions;
    }

    @Override
    public void run() {
        try {
//            Log.e("mInstructions:",mInstructions);
//            byte[] bytes = HexUtils.hexString2Bytes(mInstructions);
            if (mOutput != null && mInstructions != null) {
                mOutput.write(mInstructions);
                mOutput.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                mOutput.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void setmInstructions(byte[] instructions) {
        this.mInstructions = instructions;
    }
}
